package com.atguigu.staservice.service.impl;

import com.atguigu.commonutils.R;
import com.atguigu.staservice.entity.StatisticsDaily;
import com.atguigu.staservice.feign.UCenterClient;
import com.atguigu.staservice.mapper.StatisticsDailyMapper;
import com.atguigu.staservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-06-24
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    UCenterClient uCenterClient;
    @Override
    public void countRegister(String day) {
        //远程调用获取注册统计人数
        R r = uCenterClient.countRegister(day);
        Integer register = (Integer) r.getData().get("register");
        //添加数据之前，先删除旧的数据，保持同一个日期只有一条记录，处了删除再添加，也可以直接修改数据
        QueryWrapper<StatisticsDaily> wrapper=new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
        baseMapper.delete(wrapper);
        //将统计人数保存到统计分析表中
        StatisticsDaily daily=new StatisticsDaily();
        daily.setRegisterNum(register);
        //要统计的那天日期
        daily.setDateCalculated(day);
        //模拟登陆数、视频观看人数、课程学习人数
        daily.setLoginNum(RandomUtils.nextInt(100,200));
        daily.setVideoViewNum(RandomUtils.nextInt(100,200));
        daily.setCourseNum(RandomUtils.nextInt(100,200));
        baseMapper.insert(daily);
    }

    /**
     *
     * @param type  统计因子，前端命名与数据库保持一致比较方便
     * @param begin
     * @param end
     * @return
     */
    @Override
    public Map<String, Object> showData(String type, String begin, String end) {
        QueryWrapper<StatisticsDaily> wrapper=new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end).select("date_calculated",type);
        List<StatisticsDaily> statisticsDailies = baseMapper.selectList(wrapper);
        //在后端，Json是字符串，而在前端，JSon才是对象，对应java普通对象，转成Json对象是["aa": "bb"]格式，对应List数组，转成Json对象是["aa","bb"]
        //封装日期范围
        List<String> dateCalculated=new ArrayList<>();
        //封装统计因子数据
        List<Integer> numberList=new ArrayList<>();
        for (StatisticsDaily daily : statisticsDailies) {
            dateCalculated.add(daily.getDateCalculated());
            switch (type){
                case "register_num":
                    numberList.add(daily.getRegisterNum());
                    break;
                case "login_num":
                    numberList.add(daily.getLoginNum());
                    break;
                case "video_view_num":
                    numberList.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    numberList.add(daily.getLoginNum());
                    break;
                default:
                    break;
            }
        }
        Map<String,Object> map=new HashMap<>();
        map.put("date_calculated",dateCalculated);
        map.put("number",numberList);
        return map;
    }
}
